﻿using System;
using System.Windows.Media;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Styx.Plugins;
using Styx.Common;
using Styx;
using Styx.WoWInternals.WoWObjects;
using System.Diagnostics;
using Styx.WoWInternals;
using Styx.CommonBot;

namespace LikeASir
{
    public class LikeASir : HBPlugin
    {
        public override string Name { get { return "LikeASir-s"; } }
        public override Version Version { get { return new Version(0,1); } }
        public override string Author { get { return "swordsfish"; } }
        private static LocalPlayer Me = StyxWoW.Me;
        public override string ButtonText { get { return "no effect (yet)"; } }
        public override void Initialize() { Logging.Write(Colors.Aqua, "+ [LikeASir]"); }
        private Stopwatch _timer = new Stopwatch();
        
        public override void Pulse()
        {
            if (!Me.IsActuallyInCombat)
            {                
                if(_time() && _target())
                {
                    _emote();
                    _reset();
                }
            }
        }


        

        #region internals
        private void _reset()
        {
            TimeSpan t = new TimeSpan(0, 0, 10, 0, 0); //10min blacklist
            double temp = _timer.ElapsedMilliseconds / 1000;
            Logging.WriteDiagnostic("resetting timer after " + (temp) + " seconds");
            _timer.Reset();
            Blacklist.Add(Me.CurrentTarget.Guid, t);
            Me.ClearTarget();
        }
        private void _emote()
        {
            Lua.DoString("wave");
        }
        
        private bool _target()
        {
            if (Me.CurrentTarget.IsFriendly)
            {
                return true;
            }

            WoWUnit newTarget = (from o in ObjectManager.ObjectList
                                 where o is WoWUnit
                                 let unit = o.ToUnit()
                                 where
                                     unit.Distance <= 30
                                     && unit.IsAlive
                                     && unit.IsPlayer
                                     && unit.IsFriendly
                                     && !unit.IsPet
                                     && !Blacklist.Contains(unit.Guid)
                                 orderby unit.Distance ascending
                                 select unit
                        ).FirstOrDefault();

            // now make best selection from list
            newTarget.Target(); 

            return false;
        }

        private bool _time()
        {
            if (_timer.IsRunning && _timer.ElapsedMilliseconds > 3000)
            {
                _timer.Stop();
                Logging.Write("[LikeASir] Timer stopping");
                return true;
            }
            else if (_timer.IsRunning)
            {
                return false;
            }
            else if (!_timer.IsRunning && Me.CurrentTarget.IsFriendly && Me.CurrentTarget.IsPlayer)
            {
                _timer.Start();
                Logging.Write(Colors.Aqua, "[LikeASir] Timer starting");
                return false;
            }
            else
                return false;
        }
        #endregion
       
    }
}
